package com.wxweven.algorithm.array.bsearch;

/**
 * @author wxweven
 */
public class 有序数组二分查找 {

    public int find(int[] arrs, int target) {
        if (arrs == null || arrs.length == 0) {
            return -1;
        }

        int left = 0;
        int right = arrs.length - 1;

        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if (arrs[mid] == target) {
                return mid;
            } else if (arrs[mid] < target) {
                left = mid + 1;
            } else if (target < arrs[mid]) {
                right = mid - 1;
            }
        }

        return -1;
    }
}